package com.syher.quartz.task.job;

import com.syher.quartz.common.ConstantValue;
import com.syher.quartz.core.config.pool.task.ThreadTask;
import com.syher.quartz.core.config.quartz.job.AsyncJob;
import com.syher.quartz.core.config.quartz.job.SyncJob;
import com.syher.quartz.task.model.TaskInfo;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * Created by shenyuhang on 2018/3/7.
 */
@Component
public class TaskJob extends SyncJob {

    @Autowired
    ThreadPoolExecutor poolExecutor;

    @Override
    public void doExecute(JobExecutionContext context) throws JobExecutionException {
        TaskInfo task = (TaskInfo) context.getJobDetail()
                .getJobDataMap().get(ConstantValue.DEFAULT_TASK_JOB);

        runTask(task);
    }

    private void runTask(TaskInfo task) {
        poolExecutor.execute(new ThreadTask(task.getName()));
    }
}
